package com.lixin.smartclassroom.ui.home.tab1.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNet
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.adddianping
import com.lixin.smartclassroom.api.addrenwupinglun
import com.lixin.smartclassroom.api.groupdetail
import com.lixin.smartclassroom.api.renwupllist
import com.lixin.smartclassroom.api.shijuanxiangqing
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.commonEditTextDialog
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivityCommentDetailBinding
import com.lixin.smartclassroom.ext.chapterPosition
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.answer
import com.lixin.smartclassroom.ui.home.adapter.comment
import com.lixin.smartclassroom.ui.home.tab2.activity.LookAnswerAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentInt
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.load
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible

/**
 * Description 评价详情
 * Author: lwc
 * Date:2023/12/7 16:38
 */
class CommentDetailAct : BaseActivity<ActivityCommentDetailBinding>() {
    override fun getViewBinding() = ActivityCommentDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    private lateinit var paperList: ArrayList<DataListBean>
    private var seeType = "1"//1自己   2 他人
    private var fromType = "1"//3  答题结果
    private var taskId = ""
    private var xiaojieid = ""
    private var plType = "1"
    private var hasDianPingFlag = false//是否点评过

    override fun title(): String {
        fromType = getIntentString("fromAct")
        return when (fromType) {
            "3" -> "答题结果"
            "4" -> "批阅完成"
            else -> "任务评价"
        }
        return super.title()
    }

    override fun iniView() {

        receiveEvent<String>("refreshButtonUI") {
            binding.apply {
                btnContinue.isClickable = true
                btnContinue.visible()
                btnContinue.setRoundRectBg(
                    Color.parseColor("#4B6AEF"),
                    dp2px(27f).toInt()
                )
            }

        }

        receiveEvent<String>("refreshData") {
            getPaperData()
        }


        paperList = ArrayList()
        xiaojieid = getIntentString("xiaojieid")
        seeType = getIntentString("type")
        taskId = getIntentString("id")
        binding.apply {
            when (user_role) {
                1 -> {
                    btnContinue.text = "查看答案"
                    llBottom.visible()
                }

                2 -> {
                    when (seeType) {
                        "1" -> {
                            btnContinue.text = "查看答案"
                            tvStudentMessage.gone()
//                            llBottom.gone()

                        }

                        "2" -> {
                            btnContinue.text = "我也去答题"
                            tvStudentMessage.visible()
                            llBottom.visible()
                            llDaTiKa.gone()
                        }
                    }
                }

                3 -> {
                    tvTeacherComment.visible()
                    btnContinue.text = "提交"
                    btnGoReview.visible()
                    setButtonUI()
                }
            }

            when (fromType) {
                "3" -> {
                    btnBack.visible()
                    btnBack.setRoundRectBg(Color.parseColor("#E2E7FF"), dp2px(27f).toInt())
                    btnContinue.text = "查看答案"
                }

                else -> {
                    btnBack.gone()
                }
            }

            btnContinue.setRoundRectBg(Color.parseColor("#4B6AEF"), dp2px(27f).toInt())
            tvQuestionNum.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(11f).toInt())
            tvTeacherComment.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(10f).toInt())
            etComment.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(20f).toInt())
            llTop.setRoundRectBg(Color.parseColor("#EAEEFF"), dp2px(10f).toInt())
            circleProgress.setMax(100)

        }
        getPaperData()
        binding.recyclerAnswer.answer().apply {
            R.id.tvQuestionNo.onClick {
                R.id.tvQuestionNo.onClick {
                    start(LookAnswerAct::class.java) {
                        putExtra("paperList", paperList)
                        putExtra("currentPosition", modelPosition)
                        putExtra("id", taskId)
                        putExtra("xiaojieid", xiaojieid)
                        putExtra("fromAct", fromType)
                    }
                }
            }
        }.models = paperList
        binding.recyclerComment.comment(2)

        binding.apply {
            refresh.onRefresh {
                scopeNetLife {
                    val baseBean = renwupllist(taskId, index, 10)
                    if (baseBean.result == "0") {
                        if (index == 1) {
                            tvCommentCount.text = "精彩评论(" + baseBean.totalCount + ")"
                        }
                        listBean = baseBean.dataList!!
                        addData(listBean) {
                            if (binding.recyclerComment.bindingAdapter.models?.size == 0) {
                                binding.state.showEmpty()
                                binding.recyclerComment.gone()
                            } else {
                                binding.recyclerComment.visible()
                            }
                            listBean.size == 10
                        }
                    } else {
                        toast(baseBean.resultNote.toString())
                    }
                }
            }.autoRefresh()
        }
    }

    /**
     *@Description 设置按钮ui
     *@date 创建时间2024/2/29
     *
     */
    private fun setButtonUI() {
        binding.apply {
            when (fromType) {
                "4" -> {//批阅完成
                    btnContinue.gone()
                    btnGoReview.gone()
                }
            }
        }
    }


    @SuppressLint("NotifyDataSetChanged")
    private fun getPaperData() {
        binding.apply {
            scopeNetLife {
                val baseBean = groupdetail(taskId)
                if (baseBean!!.result == "0") {
                    tvQuestionNum.text = "共" + baseBean.timucount + "题"
                    tvTitle.text = baseBean.name
                    tvTime.text = "提交时间：" + baseBean.createdate
                    tvClass.text = "所属班级：" + baseBean.classname
                    tvTotalScore.text = "总分" + baseBean.totalscouce
                    tvCyrs.text = baseBean.cyrenshu
                    tvScore.text = baseBean.pingjun
                    tvContent.text = baseBean.classname + " " + "第" + chapterPosition + "小节"

                    tvRank.text = baseBean.paiming



                    if (baseBean.defen.isNullOrEmpty() || baseBean.defen == "0" || baseBean.totalscouce == "0" || baseBean.totalscouce.isNullOrEmpty()) circleProgress.setCurrent(
                        0
                    )
                    else circleProgress.setCurrent(
                        baseBean.defen.toDouble().toInt() / baseBean.totalscouce.toDouble()
                            .toInt() * 100
                    )


                    when (baseBean.ifzudui) {
                        "1" -> {
                            tvStudentMessage.text =
                                "组别：" + baseBean.groupname + "\n" + "组员：" + baseBean.groupcustomer
                        }

                        "2" -> {
                            tvStudentMessage.text = "学生：" + baseBean.groupname
                        }
                    }


                    when (user_role) {
                        1, 2 -> {
                            if (baseBean.dianping.isNullOrEmpty()) tvTeacherComment.gone()
                            else {
                                tvTeacherComment.visible()
                                tvTeacherComment.text = baseBean.dianping
                            }
                        }

                        3 -> {
                            if (baseBean.dianping.isNullOrEmpty()) {
                                tvTeacherComment.visible()
                                tvTeacherComment.isClickable = true
                                btnContinue.isClickable = false
                                hasDianPingFlag = false
                                btnContinue.setRoundRectBg(
                                    Color.parseColor("#999999"),
                                    dp2px(27f).toInt()
                                )
                                btnContinue.gone()
                                tvTeacherComment.text = "点击后输入点评信息"
                            } else {
                                tvTeacherComment.visible()
                                tvTeacherComment.isClickable = false
                                hasDianPingFlag = true
                                btnContinue.gone()
//                                    btnContinue.isClickable = true
//                                    btnContinue.setRoundRectBg(
//                                        Color.parseColor("#4B6AEF"),
//                                        dp2px(27f).toInt()
//                                    )
                                tvTeacherComment.text = baseBean.dianping
                            }
                        }
                    }

                    when (baseBean.state) {
                        "1" -> tvReviewState.text = "待批阅"
                        "2" -> tvReviewState.text = "批阅中"
                        "3" -> tvReviewState.text = baseBean.defen + "分"
                    }

                }

                paperList.clear()
                paperList.addAll(shijuanxiangqing(xiaojieid, taskId)!!)
                binding.recyclerAnswer.bindingAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun onClick() {
        binding.apply {
            btnContinue.onClick {
                if (paperList.size != 0) {
                    if (user_role == 2) {
                        start(LookAnswerAct::class.java) {
                            putExtra("paperList", paperList)
                            putExtra("currentPosition", 0)
                            putExtra("id", taskId)
                            putExtra("xiaojieid", xiaojieid)
                        }

                    } else if (user_role == 3) {

                        if (!hasDianPingFlag) {
                            scopeNetLife {
                                val baseBean = adddianping(taskId, tvTeacherComment.text.toString())
                                if (baseBean.result == "0") {
                                    saveSuccessDialog("评价成功")
                                    btnContinue.gone()
                                } else {
                                    toast(baseBean.resultNote.toString())
                                }
                            }
                        }
                    } else {
                        start(LookAnswerAct::class.java) {
                            putExtra("paperList", paperList)
                            putExtra("currentPosition", 0)
                            putExtra("id", taskId)
                            putExtra("xiaojieid", xiaojieid)
                            putExtra("fromAct", fromType)
                        }
                    }
                } else {
                    toast("本次答题数据异常")
                }
            }

            btnBack.onClick {
                finish()
            }

            tvHideName.setOnCheckedChangeListener { _, _ ->
                tvHideName.isSelected = !tvHideName.isChecked
                plType = if (tvHideName.isSelected) {
                    "1"
                } else {
                    "2"
                }
            }

            tvTeacherComment.onClick {
                if (user_role == 3 && !hasDianPingFlag) {
                    commonEditTextDialog(
                        2,
                        taskId,
                        "编辑评语",
                        "请输入评语",
                        "取消",
                        "确定",
                        textview = tvTeacherComment
                    )
                }

            }

            btnGoReview.onClick {
                start(LookAnswerAct::class.java) {
                    putExtra("paperList", paperList)
                    putExtra("currentPosition", 0)
                    putExtra("id", taskId)
                    putExtra("xiaojieid", xiaojieid)
                }
            }


            btnSend.onClick {
                if (etComment.text().isEmpty()) {
                    toast("请输入评论内容")
                    return@onClick
                }
                scopeNetLife {
                    addrenwupinglun(taskId, etComment.text(), plType)
                    saveSuccessDialog("评论成功")
                    etComment.setText("")
                    binding.refresh.autoRefresh()
                }
            }
        }
    }
}